import { HttpException, HttpStatus, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Connection, Repository } from "typeorm";
import { RoleService } from "./role.service";
import { UsersEntity } from "../Entity/user.entity";


@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(UsersEntity)
    private readonly usersResposity: Repository<UsersEntity>,
    private readonly roleService: RoleService
  ) { }



  async create(user): Promise<UsersEntity[]> {
    const u = await this.findOne(user.username);

    if (u) {
      throw new HttpException({
        message: 'Input data validation failed',
        error: 'name must be unique',
      },
        HttpStatus.BAD_REQUEST);
    }
    return await this.usersResposity.save(user);

  }

  async findOne(username: string): Promise<UsersEntity> {
    let list = await this.usersResposity.find({ relations: ['role'] });

    const roleList = await this.roleService.findAll();
    let temp = list.find(item => item.username === username);
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      const item = roleList.find(role => role.id == element.id);
      element.role = item;
    }

    return temp;
  }

  async getUserById(userId: number): Promise<UsersEntity> {
    return this.usersResposity.findOne({ where: { id: userId } });
  }


  async findAll(): Promise<UsersEntity[]> {
    let list = await this.usersResposity.find({ relations: ['role'] });

    const roleList = await this.roleService.findAll();
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      const item = roleList.find(role => role.id == element.id);
      element.role = item;
      // element.role = await this.roleService.findOne(element.role.rname);
      // for (let j = 0; j < element.role.length; j++) {
      //     const item = await this.menuService.findOne(element.menu[j].id);
      //     element.menu[j].children = item.children;
      // }
    }

    return list;
  }

  async remove(id: number): Promise<void> {
    await this.usersResposity.delete(id);
  }

  async update(user: UsersEntity): Promise<void> {
    await this.usersResposity.update(user.id, user);
  }

  // async findOne(username: string): Promise<User | undefined> {
  //   return this.users.find(user => user.username === username);
  // }

  // async deleteUser(userId: number) {
  //   console.log(userId);
  //   const user = this.users.find(obj => obj.userId == userId);
  //   console.log(user);
  //   if (user) {
  //     const index = this.users.indexOf(user);
  //     if (index > -1) {
  //       this.users.splice(index, 1);
  //     }
  //     return true;
  //   }
  //   return false;
  // }

  // async updateUser(user: User) {
  //   if (user) {
  //     const updateUser = this.users.find(obj => obj.userId == user.userId);
  //     const index = this.users.indexOf(updateUser);
  //     console.log(index);
  //     this.users[index].username = user.username;
  //     this.users[index].password = user.password;
  //     return true;
  //   }
  //   return false;

  // }



}